package com.cndw.kungfu.model.message;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cndw.kungfu.dao.MailDao;
import com.cndw.kungfu.domain.Mail;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.vo.MailVO;

/**
 * 信件管理
 * @author fantadust
 * @date 2011-5-11 下午04:11:22
 */
@Component
public class MailManager {
	@Autowired
	private MailDao dao;
	
	/**
	 * 插入信件
	 * @param mail
	 * @return
	 */
	public int newMail(Mail mail){
		try {
			return dao.insert(mail).intValue();
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}		
	}
	/**
	 * 获取列表
	 * @param playerId
	 * @param type
	 * @param offset
	 * @param limit
	 * @return
	 */
	public List<MailVO> getList(long playerId,int type,int action,int offset,int limit){
		
		try {
			
			List<Mail> list = dao.select(playerId,type,action,offset,limit);
			
			List<MailVO> mailVOs = new ArrayList<MailVO>();
			for (Mail mail : list) {
				mailVOs.add(mail.toMailVO());
			}
			return mailVOs;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public int getcount(long playerId,int type,int action){		
		try {
			return dao.getCount(playerId, type,action);
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return 0;
	}
	
	
	/**
	 * 获取信件
	 * @param playerId
	 * @param id
	 * @return
	 */
	public Mail getReceivedMail(long playerId,int id){
		try {
			return dao.getReceivedMail(playerId, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取信件
	 * @param playerId
	 * @param id
	 * @return
	 */
	public Mail getSendedMail(long playerId,int id){
		try {
			return dao.getSendedMail(playerId, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 标记已读
	 * @param playerId
	 * @param ids
	 */
	public void signReaded(long playerId,int[] ids){		
		try {
			dao.signReaded(playerId, ids);
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	public void signReaded(long playerId,int id){		
		try {
			dao.signReaded(playerId, id);
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}	
	
	public void saveMail(long playerId,int id){
		try {
			dao.signSaved(playerId, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	public void delMail(long playerId,int action,int[] ids){
		try {
			if (action == 2) {//发件箱
				dao.signSendDel(playerId,ids);
			}else {
				dao.signDel(playerId,ids);
			}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public void delAll(long playerId,int action,int type){
		try {
			if (action == 1) {//发件箱
				dao.delAll(playerId, type,0);
			}else if (action == 2) {				
				dao.delSendAll(playerId, type);	
			}else{
				dao.delAll(playerId, type,1);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void receiveAttachment(long playerId,int mailId){
		try {
			dao.receiveAttachment(playerId, mailId, TimeProcesser.getUnixTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public List<Mail> getMailsGm(long playerId,int action,boolean haveAtt,boolean gotAtt,int offset,int limit){
		try {
			List<Mail> list = dao.getMailsGM(playerId,action,haveAtt,gotAtt,offset,limit);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public int getCountGm(long playerId,int action,boolean haveAtt,boolean gotAtt){
		try {
			return dao.getMailsGMCout(playerId,action,haveAtt,gotAtt);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	public void clearData() {
		try {
			dao.clearData20();
			dao.clearData5();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
